package com.opencloud.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opencloud.common.model.PageParams;
import com.opencloud.common.model.Result;
import com.opencloud.system.client.model.UserInfo;
import com.opencloud.system.client.model.entity.SystemDeveloper;
import com.opencloud.system.client.model.entity.SystemRole;
import com.opencloud.system.client.service.ISystemDeveloperServiceClient;
import com.opencloud.system.service.SystemDeveloperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统用户信息
 *
 * @author liuyadu
 */
@Api(tags = "系统用户管理")
@RestController
public class SystemDeveloperController implements ISystemDeveloperServiceClient {
    @Autowired
    private SystemDeveloperService systemDeveloperService;


    @ApiOperation(value = "获取账号登录信息", notes = "仅限系统内部调用")
    @PostMapping("/developer/login")
    @Override
    public Result<UserInfo> login(@RequestParam(value = "username") String username,@RequestParam(value = "thirdParty",required = false)  String thirdParty) {
        UserInfo account = systemDeveloperService.login(username,thirdParty);
        return Result.ok().data(account);
    }

    @ApiOperation(value = "获取开发者列表", notes = "获取开发者列表")
    @GetMapping("/developer")
    @Override
    public Result<Page<SystemDeveloper>> getPage(@RequestParam Map<String, Object> map) {
        return Result.ok().data(systemDeveloperService.findPage(new PageParams(map)));
    }

    @ApiOperation(value = "获取开发者列表", notes = "获取开发者列表")
    @GetMapping("/developer/list")
    @Override
    public Result<List<SystemDeveloper>> getList() {
        return Result.ok().data(systemDeveloperService.findList());
    }


    @ApiOperation(value = "添加/修改开发者信息", notes = "添加/修改开发者信息")
    @PostMapping("/developer/save")
    @Override
    public Result<Long> save(@RequestBody SystemDeveloper systemDeveloper) {
        if (systemDeveloper.getUserId() == null) {
            systemDeveloperService.add(systemDeveloper);
        } else {
            systemDeveloperService.update(systemDeveloper);
        }
        return Result.ok();
    }

    @ApiOperation(value = "修改用户密码", notes = "修改用户密码")
    @PostMapping("/developer/update/password")
    @Override
    public Result updatePassword(@RequestParam(value = "userId") Long userId, @RequestParam(value = "password") String password) {
        systemDeveloperService.updatePassword(userId, password);
        return Result.ok();
    }

    @ApiOperation(value = "注册第三方系统登录账号", notes = "仅限系统内部调用")
    @PostMapping("/developer/add/thirdParty")
    @Override
    public Result addThirdParty(
            @RequestParam(value = "account") String account,
            @RequestParam(value = "password") String password,
            @RequestParam(value = "accountType") String accountType,
            @RequestParam(value = "nickName") String nickName,
            @RequestParam(value = "avatar") String avatar
    ) {
        SystemDeveloper developer = new SystemDeveloper();
        developer.setNickName(nickName);
        developer.setUserName(account);
        developer.setPassword(password);
        developer.setAvatar(avatar);
        systemDeveloperService.addThirdParty(developer, accountType);
        return Result.ok();
    }

}
